package com.xxx.yuedong.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author HG
 * @version 1.0
 **/
@Component
public class EventMsgSendUtil {
    private static RabbitTemplate rabbitTemplate;

    /**
     * //@Autowired：可以自动装配变量、方法以及构造器
     * 此构造方法使用自动装配注解为本类的静态变量rabbitTemplate装配并使之能够被sendEventMessage静态方法使用
     * 注：静态方法和静态变量都不能被@Autowired装配
     * @param rabbitTemplate
     */
    @Autowired
    public EventMsgSendUtil(RabbitTemplate rabbitTemplate) {
        EventMsgSendUtil.rabbitTemplate = rabbitTemplate;
    }

    /**
     *
     * @param userId 事件发出者ID
     * @param objectId 对象ID
     * @param event 事件
     */
    public static void sendEventMsg(Long userId, Long objectId, String event) {
        JSONObject msg = new JSONObject();
        msg.put("userId", userId);
        msg.put("objectId", objectId);
        msg.put("event", event);

        rabbitTemplate.convertAndSend("eventExchange", event, msg);
    }
}
